<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/homeone' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片试图 -->
    <el-card>
      <!-- 添加角色按钮区域 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="addDialogVisible = true"
            >添加角色</el-button
          >
        </el-col></el-row
      >
      <!-- 角色列表区 -->
      <el-table :data="roleList" border stripe>
        <!-- 展开列 -->
        <el-table-column type="expand"></el-table-column>
        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"> </el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template>
            <el-button size="mini" type="primary" icon="el-icon-edit"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" icon="el-icon-delete"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加用户的对话框 -->
    <el-dialog title="添加用户" :visible.sync="addDialogVisible" width="50%">
      <!-- 内容主体区 -->
      <!-- 1. -->
      <el-form
        :model="addFrom"
        :rules="addFromRules"
        ref="addFromRef"
        label-width="70px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addFrom.username"></el-input>
        </el-form-item>
      </el-form>
      <!-- 2. -->
      <el-form
        :model="addFrom"
        :rules="addFromRules"
        ref="addFromRef"
        label-width="70px"
      >
        <el-form-item label="密码" prop="password">
          <el-input v-model="addFrom.password"></el-input>
        </el-form-item>
      </el-form>
      <!-- 3. -->
      <el-form
        :model="addFrom"
        :rules="addFromRules"
        ref="addFromRef"
        label-width="70px"
      >
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addFrom.email"></el-input>
        </el-form-item>
      </el-form>
      <!-- 4. -->
      <el-form
        :model="addFrom"
        :rules="addFromRules"
        ref="addFromRef"
        label-width="70px"
      >
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="addFrom.mobile"></el-input>
        </el-form-item>
      </el-form>

      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    // 验证邮箱规则
    var checkEmail = (rule, value, cb) => {
      const regEmail =
        /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/

      if (regEmail.test(value)) {
        // 邮箱合法
        return cb()
      } else {
        cb(new Error('请输入合法的邮箱'))
      }
    }
    // 验证手机号规则
    var checkMobile = (rule, value, cb) => {
      const regMoblie =
        /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
      if (regMoblie.test(value)) {
        // 手机号合法
        return cb()
      } else {
        cb(new Error('请输入合法的手机号'))
      }
    }
    return {
      // 所有角色的列表数据
      roleList: [],
      // 隐藏添加用户信息的对话框
      addDialogVisible: false,
      // 添加用户的表单数据
      addFrom: {
        username: '',
        password: '',
        email: '',
        mobile: '',
      },
      // 添加表单的验证规则对象
      addFromRules: {
        username: [
          {
            username: [
              {
                required: true,
                message: '请输入用户名',
                trigger: 'blur',
              },
              {
                min: 3,
                max: 5,
                message: '长度在 3 到 5 个字符',
                trigger: 'blur',
              },
            ],
            password: [
              {
                required: true,
                message: '请输入密码',
                trigger: 'blur',
              },
              {
                min: 6,
                max: 15,
                message: '长度在 6 到 15 个字符',
                trigger: 'blur',
              },
            ],
            email: [
              {
                required: true,
                message: '请输入邮箱',
                trigger: 'blur',
              },
              { validator: checkEmail, trigger: 'blur' },
            ],

            mobile: [
              {
                required: true,
                message: '请输入手机号',
                trigger: 'blur',
              },

              { validator: checkMobile, trigger: 'blur' },
            ],
          },
        ],
      },
    }
  },
  created() {
    this.getRolesList()
  },
  methods: {
    // 获取所有角色的列表
    async getRolesList() {
      const { data: res } = await this.$http.get('roles')

      if (res.meta.status !== 200) {
        return this.$message.error('获取数据失败！')
      } else {
        this.roleList = res.data
        console.log(this.roleList)
      }
    },
    //点击按钮，添加新用户
    addUser() {
      this.$refs.addFromRef.validate(async (valid) => {
        // console.log(valid)
        if (!valid) return
        // 可以发起添加用户的网络请求
        const { data: res } = await this.$http.post('users', this.addFrom)
        if (res.meta.status != 201) {
          this.$message.error('添加用户失败')
        } else {
          this.$message.success('添加用户成功')
          //隐藏用户的对话框
          this.addDialogVisible = false
          // 重新获取用户的列表数据
          this.getUserList()
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped></style>
